View Javadoc
1   package org.apache.maven.surefire.testng.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import org.apache.maven.surefire.group.match.AndGroupMatcher;
26  import org.apache.maven.surefire.group.match.GroupMatcher;
27  import org.apache.maven.surefire.group.match.InverseGroupMatcher;
28  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
29  import org.apache.maven.surefire.group.parse.ParseException;
30  
31  import org.testng.IMethodSelector;
32  import org.testng.IMethodSelectorContext;
33  import org.testng.ITestNGMethod;
34  
35  /**
36   * Method selector delegating to {@link GroupMatcher} to decide if a method is included or not.
37   *
38   */
39  public class GroupMatcherMethodSelector
40      implements IMethodSelector
41  {
42  
43      private static final long serialVersionUID = 1L;
44  
45      private static GroupMatcher matcher;
46  
47      private Map<ITestNGMethod, Boolean> answers = new HashMap<ITestNGMethod, Boolean>();
48  
49      public boolean includeMethod( IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod )
50      {
51          // System.out.println( "Checking: " + method + " vs. matcher: " + matcher );
52          Boolean result = (Boolean) answers.get( method );
53          if ( result != null )
54          {
55              // System.out.println( "Enabled? " + result );
56              return result;
57          }
58  
59          if ( matcher == null )
60          {
61              // System.out.println( "No matcher, enable by default" );
62              return true;
63          }
64  
65          String[] groups = method.getGroups();
66          result = Boolean.valueOf( matcher.enabled( groups ) );
67  
68          answers.put( method, result );
69  
70          // System.out.println( "Enabled? " + result );
71          return result;
72      }
73  
74      public void setTestMethods( List<ITestNGMethod> testMethods )
75      {
76      }
77  
78      public static void setGroups( String groups, String excludedGroups )
79      {
80          // System.out.println( "Processing group includes: '" + groups + "'\nExcludes: '" + excludedGroups + "'" );
81  
82          try
83          {
84              AndGroupMatcher matcher = new AndGroupMatcher();
85              GroupMatcher in = null;
86              if ( groups != null && groups.trim().length() > 0 )
87              {
88                  in = new GroupMatcherParser( groups ).parse();
89              }
90  
91              if ( in != null )
92              {
93                  matcher.addMatcher( in );
94              }
95  
96              GroupMatcher ex = null;
97              if ( excludedGroups != null && excludedGroups.trim().length() > 0 )
98              {
99                  ex = new GroupMatcherParser( excludedGroups ).parse();
100             }
101 
102             if ( ex != null )
103             {
104                 matcher.addMatcher( new InverseGroupMatcher( ex ) );
105             }
106 
107             if ( in != null || ex != null )
108             {
109                 // System.out.println( "Group matcher: " + matcher );
110                 GroupMatcherMethodSelector.matcher = matcher;
111             }
112         }
113         catch ( ParseException e )
114         {
115             throw new IllegalArgumentException(
116                 "Cannot parse group includes/excludes expression(s):\nIncludes: " + groups + "\nExcludes: "
117                     + excludedGroups, e );
118         }
119     }
120 
121     public static void setGroupMatcher( GroupMatcher matcher )
122     {
123         GroupMatcherMethodSelector.matcher = matcher;
124     }
125 
126 }